package br.com.registrofinancas.view;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

import br.com.registrofinancas.R;
import br.com.registrofinancas.adapters.CalendarioAdapter;
import br.com.registrofinancas.adapters.CalendarioHeaderAdapter;
import br.com.registrofinancas.database.MovimentosEntradaDAO;
import br.com.registrofinancas.database.MovimentosSaidaDAO;
import br.com.registrofinancas.util.CurrencyUtils;
import br.com.registrofinancas.util.GregorianCalendarUtil;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class CalendarioActivity extends Activity implements OnClickListener{
	private GregorianCalendar mes;
	private GridView gvCalendario;
	private CalendarioAdapter adaptador;
	private GridView gvHeaderCalendario;
	private CalendarioHeaderAdapter descricaoDiasAdaptor;
	private TextView mesAnoHeaderNavegacao;
	private ImageView imagePrevious;
	private ImageView imageNext;
	private SimpleDateFormat formatoMesAnoHeaderNavegacao;
	private Handler handler;
	private ProgressDialog progressoUpdateCalendar;
	private TextView tvTotalGanhos;
	private TextView tvTotalGastos;
	private TextView tvSaldo;
	private boolean updateView = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_calendar);
		
		mes = (GregorianCalendar) GregorianCalendar.getInstance().clone();
		handler = new Handler();
		adaptador = new CalendarioAdapter(this, mes, handler, runnableUpdateView);
		gvCalendario = (GridView) findViewById(R.id.activity_calendar_gv_calendario);
		gvCalendario.setAdapter(adaptador);
	
		descricaoDiasAdaptor = new CalendarioHeaderAdapter(this,GregorianCalendarUtil.getDiasLocalizacao(this));		
		gvHeaderCalendario = (GridView) findViewById(R.id.activity_calendar_gv_header_calendario);
		gvHeaderCalendario.setAdapter(descricaoDiasAdaptor);
		
		mesAnoHeaderNavegacao = (TextView) findViewById(R.id.activity_calendar_navegation_header_tv_mesano);
		formatoMesAnoHeaderNavegacao = new SimpleDateFormat("MMM/yyyy");
		mesAnoHeaderNavegacao.setText(formatoMesAnoHeaderNavegacao.format(mes.getTime()).toUpperCase());
		
		imagePrevious = (ImageView) findViewById(R.id.activity_calendar_navegation_header_iv_anterior);
		imagePrevious.setOnClickListener(this);
		imagePrevious.setClickable(false);
		imageNext = (ImageView) findViewById(R.id.activity_calendar_navegation_header_iv_proxima);
		imageNext.setOnClickListener(this);
		imageNext.setClickable(false);
				
		tvTotalGanhos = (TextView) findViewById(R.id.activity_calendar_tv_total_ganhos);
		tvTotalGastos = (TextView) findViewById(R.id.activity_calendar_tv_total_gastos);
		tvSaldo = (TextView) findViewById(R.id.activity_calendar_tv_saldo);
		
		updateCalendar();
	}
	
	@Override
	protected void onResume(){
		super.onResume();
		if(updateView){
			updateCalendar();
			updateView = false;
		}
	}
	
	@Override
	protected void onPause(){
		super.onPause();
		updateView = true;
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.activity_calendar_navegation_header_iv_anterior:
			setPreviousMonth();
			break;
		case R.id.activity_calendar_navegation_header_iv_proxima:
			setNextMonth();
			break;
		default:
			break;
		}
		//adaptador.preencheDias();
	}
	
	private void setNextMonth( ) {
		if (mes.get( GregorianCalendar.MONTH ) == mes.getActualMaximum( GregorianCalendar.MONTH )) {
			mes.set( ( mes.get( GregorianCalendar.YEAR ) + 1 ),	mes.getActualMinimum( GregorianCalendar.MONTH ), 1) ;
		}
		else {
			mes.set( GregorianCalendar.MONTH, mes.get( GregorianCalendar.MONTH ) + 1) ;
		}
		updateCalendar();
	}

	private void setPreviousMonth( ) {
		if (mes.get( GregorianCalendar.MONTH ) == mes.getActualMinimum( GregorianCalendar.MONTH )) {
			mes.set( ( mes.get( GregorianCalendar.YEAR ) - 1 ), mes.getActualMaximum( GregorianCalendar.MONTH ), 1) ;
		}
		else {
			mes.set( GregorianCalendar.MONTH, mes.get( GregorianCalendar.MONTH ) - 1 ) ;
		}
		updateCalendar();
	}

	private void updateCalendar(){
		imageNext.setClickable(false);
		imagePrevious.setClickable(false);
		
		progressoUpdateCalendar = ProgressDialog.show(this, "Aguarde", "Atualizando ...");
		new Thread(new Runnable() {
			@Override
			public void run() {
				adaptador.preencheDias();
				progressoUpdateCalendar.dismiss();
			}
		}).start();	
		atualizaTotais();
	}
	
	private Runnable runnableUpdateView = new Runnable() {
		public void run() {
			adaptador.notifyDataSetChanged();
			mesAnoHeaderNavegacao.setText(formatoMesAnoHeaderNavegacao.format(mes.getTime()).toUpperCase());
			imagePrevious.setClickable(true);
			imageNext.setClickable(true);
			atualizaTotais();
		}
	};
	
	private void atualizaTotais(){
		MovimentosEntradaDAO entrada = new MovimentosEntradaDAO(this);
		MovimentosSaidaDAO saida = new MovimentosSaidaDAO(this);
		Double totalGanhos = entrada.getValorTotalEntradasPorMes(mes.getTime());
		Double totalGastos = saida.getValorTotalSaidasPorMes(mes.getTime());
		Double saldo = totalGanhos - totalGastos; 
		tvTotalGanhos.setText(CurrencyUtils.formataValor(totalGanhos, CalendarioActivity.this));
		tvTotalGastos.setText(CurrencyUtils.formataValor(totalGastos, CalendarioActivity.this));
		tvSaldo.setText(CurrencyUtils.formataValor(saldo, CalendarioActivity.this));
	}
	
}

